package xnj.lazydog.btcontroller.ControlViews.ControlViews;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import xnj.lazydog.btcontroller.ControlViews.Value;
import xnj.lazydog.btcontroller.ControlViews.ViewOptions;
import xnj.lazydog.btcontroller.R;

public class ControlValueBar extends ControlElement {



    public ControlValueBar (ViewOptions options)
    {
        super(options);


    }

    float bar_height;

    @Override
    public void onSizeChanged() {

        edge_pix = (w_pix)*EDGE_SIZE_RATIO;

        if(edge_pix>max_edge_pix)
            edge_pix = max_edge_pix;

        if(edge_pix<min_edge_pix)
            edge_pix = min_edge_pix;

        p.setStrokeWidth(edge_pix);

        rectF.right = w_pix*0.5f;
        rectF.left = -rectF.right;
        rectF.bottom = h_pix*0.5f;
        rectF.top = -rectF.bottom;

        back_rect.left = rectF.left+edge_pix;
        back_rect.right = rectF.right-edge_pix;
        back_rect.top = rectF.top+edge_pix;
        back_rect.bottom = rectF.bottom-edge_pix;

        bar_height = h_pix-edge_pix*2;

        value_bar.bottom = bar_height*0.5f;
        value_bar.left = back_rect.left;
        value_bar.right = back_rect.right;
        //value_bar.top = 0;
    }
    RectF value_bar = new RectF();

    float ratio;
    float ratio_d;
    float ratio_dt;

    @Override
    public void draw(Canvas canvas) {


        if(linkedValues[0]==null)
            ratio = 0.5f;
        else
            ratio = (linkedValues[0].getFloat()-options.off_value)/(options.on_value-options.off_value);

        if(ratio<0)
            ratio = 0;
        if(ratio>1)
            ratio = 1;

        ratio_dt =(ratio-ratio_d);
        if(Math.abs(ratio_dt)>0.003f) {
            ratio_d += ratio_dt * 0.35f;
        }else
        {
            ratio_d = ratio;
        }

        value_bar.top = value_bar.bottom-bar_height*ratio_d;

        p.setStyle(Paint.Style.FILL);
        p.setColor(THEME_WHITE);
        canvas.drawRect(rectF,p);

        p.setColor(THEME_EDGE);
        canvas.drawRoundRect(value_bar,edge_pix,edge_pix,p);

        p.setColor(0xffcccccc);
        p.setStyle(Paint.Style.STROKE);
        canvas.drawRoundRect(rectF,edge_pix,edge_pix,p);

    }


    @Override
    public void showSettingsDialog(Context context) {

        if(linkedValues[0]==null) {
            Toast.makeText(context,"请先链接一个变量",Toast.LENGTH_SHORT).show();
            return;
        }
        final EditText on_edit,off_edit;
        Button ok;
        TextView info;
        String str;
        final AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View layout = LayoutInflater.from(context).inflate(R.layout.toy_settings_value_bar,null);
        builder.setView(layout);
        dialog = builder.create();
        if(dialog.getWindow()!=null)
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        on_edit = layout.findViewById(R.id.on_edit);
        off_edit = layout.findViewById(R.id.off_edit);
        info = layout.findViewById(R.id.button_info);
        ok = layout.findViewById(R.id.button);

        if(options.on_en)
        {
            if(linkedValues[0].type<4)
                str=(""+(int)(options.on_value));
            else
                str=(""+options.on_value);
        }else
            str="";

        on_edit.setText(str);
        on_edit.setSelection(str.length());

        if(options.off_en)
        {
            if(linkedValues[0].type<4)
                str=(""+(int)(options.off_value));
            else
                str=(""+options.off_value);
        }else
            str ="";

        off_edit.setText(str);
        off_edit.setSelection(str.length());

        if(linkedValues[0].type<4)
            str = "此控件链接的"+valuesName[0]+"为"+type_name[linkedValues[0].type]+"型,数值不得超过"+(int)(MIN_VALUES[linkedValues[0].type])+"~"+(int)(MAX_VALUES[linkedValues[0].type]);
        else
            str = "此控件链接的"+valuesName[0]+"为"+type_name[linkedValues[0].type]+"型,数值不得超过"+(MIN_VALUES[linkedValues[0].type])+"~"+(MAX_VALUES[linkedValues[0].type]);

        info.setText(str);

        initLimitedEdit(on_edit,info,linkedValues[0].type);
        initLimitedEdit(off_edit,info,linkedValues[0].type);

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Value value = getNumber(on_edit.getText().toString());
                if(value.type==0)
                {
                    options.on_value = 0;
                    options.on_en = true;
                }else
                {
                    options.on_value = value.getFloat();
                    options.on_en = true;
                }

                value = getNumber(off_edit.getText().toString());
                if(value.type==0)
                {
                    options.off_value = 0;
                    options.off_en = true;
                }else
                {
                    options.off_value = value.getFloat();
                    options.off_en = true;
                }

                options.threshold = (options.on_value+options.off_value)*0.5f;
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    @Override
    public void clicked(Context context) {

        showSettingsDialog(context);

    }

    @Override
    public void dragged() {

    }

    @Override
    public void released() {

    }

    @Override
    public void pressed() {

    }

    @Override
    public void zoomed(float sx, float sy) {

    }

}
